﻿using System;
using System.Runtime.InteropServices;

namespace Eflong.Structure
{
    [StructLayout( LayoutKind.Sequential )]
    public struct CoalMineInfoItem
    {
        private const int SIZE_IPADDR = 16;
        private const int SIZE_COALMINENAME = 11;   //

        public UInt32 m_nId;

        [MarshalAs( UnmanagedType.ByValTStr , SizeConst = SIZE_COALMINENAME )]
        public string m_szName;

        [MarshalAs( UnmanagedType.ByValTStr , SizeConst = SIZE_IPADDR )]
        public string m_szIPAddr;

        [MarshalAs( UnmanagedType.U1 )]
        public bool m_bConnected;

        public static CoalMineInfoItem ConverFromByteArray( byte[ ] ba , ref int pos )
        {
            return StructConvert.PtrToStruct<CoalMineInfoItem>( ba , ref pos );
        }
    };

    [StructLayout( LayoutKind.Sequential )]
    public struct CoalMineInfoStructBase
    {
        public uint m_dwMethodType;
        public int m_nMethodAck;

        public uint m_dwBufSize;
        public uint m_dwNumOfItems;
    }

    [StructLayout( LayoutKind.Sequential )]
    public struct CoalMineInfoStruct
    {
        public CoalMineInfoStructBase baseStruct;

        public CoalMineInfoItem[ ] m_CoalMineInfoItems;

        public static CoalMineInfoStruct ConverFromByteArray( byte[ ] ba )
        {
            int pos = 0;
            CoalMineInfoStruct st = new CoalMineInfoStruct( );
            st.baseStruct = StructConvert.PtrToStruct<CoalMineInfoStructBase>( ba , ref pos );

            if ( st.baseStruct.m_dwNumOfItems >= 100 )
            {
                throw new Exception( "CoalMineInfoStruct m_dwNumOfItems>= 100 " );
            }
            st.m_CoalMineInfoItems = new CoalMineInfoItem[ st.baseStruct.m_dwNumOfItems ];
            for ( int i = 0; i < st.baseStruct.m_dwNumOfItems; i++ )
            {
                CoalMineInfoItem item = CoalMineInfoItem.ConverFromByteArray( ba , ref pos );
                st.m_CoalMineInfoItems[ i ] = item;
            }
            return st;
        }
    };

    [StructLayout( LayoutKind.Sequential )]
    public struct ConnectionStruct
    {
        //ConnectionStruct() {ZeroMemory(this, sizeof(ConnectionStruct));}
        public uint m_dwMethodType;

        public int m_nMethodAck;

        public UInt32 m_nCoalMineId;

        [MarshalAs( UnmanagedType.U1 )]
        public bool m_bConnected;

        public static ConnectionStruct ConverFromByteArray( byte[ ] ba )
        {
            return StructConvert.PtrToStruct<ConnectionStruct>( ba );
        }
    };
}